﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using CommonLib.Pattern.FSM;
using CommonLib.Util;

public class EnemyFSMStateEscape : EnemyFSMStateBase
{
	public EnemyFSMStateEscape(RoleCtrl _role, IFSM fsm = null) : base("Escape", _role , fsm)
	{
	}

	protected override void OnEnter()
	{
		base.OnEnter();
		TransitionParam tp = GetTransistionParam(TransitionParamType.Escape);
		vigilance = tp.VigilanceEscape;
	}

	protected override void OnExit()
	{
		base.OnExit();
	}

	protected override void OnUpdate()
	{
		base.OnUpdate();

		//原始目标
		Collider2D fleerTargt = target;
		//重定向范围内的目标
		if ( FindNearestDangrous(vigilance, out Collider2D col))
		{
			fleerTargt = col;
		} 

		if( null != fleerTargt)
		{
			Vector2 fleer = new Vector2(Role.transform.position.x - fleerTargt.transform.position.x,
				Role.transform.position.y - fleerTargt.transform.position.y).normalized;
			Vector2 desired = (Role.SubManagerMove.VelocityDir + fleer) .normalized;
			//role.SubManagerMove.Rb2d.velocity = desired.normalized * role.RoleEntity.CurrentSpeed;
			Role.SubManagerMove.SetVelocityDir(desired);

			//Debug.Log("EPChainNodeEscape");
		}
		else
		{
			//Debug.Log("EPChainNodeEscape Not");
		}

	}
}
